<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
console.log(11111)
import {
  // searchAccount,
  getAuthorizedAccount,
  addFactoryBuilding,
  getFactory,
  deleteFactoryBuilding,
  addWorkShop,
  getWorkShop,
  deleteWorkShop,
  authorizeFactory,
  deleteAccount
} from '../../api/clothrequest'
import { useUserStore } from '@/stores'
const userStore = useUserStore()
const accountData = ref([])
const loading = ref(false)
const loadingFactory = ref(false)
const empowerData = ref([
  {
    userName: '',
    id: '',
    factoryId: ''
  }
])

// 车间数据
const dialogWorkShop = ref(false)
const dialogFactory = ref(false)
const dialogVisible = ref(false)
const workshopName = ref([{ name: '' }])
const factoryName = ref([{ factoryName: '' }])
// const formModel = ref([{ account: '' }])
const factoryList = ref([])
const workshopList = ref([])
// 获取账号数据
const getauthorizedaccount = async () => {
  loading.value = true
  const res = await getAuthorizedAccount()
  accountData.value = res.data.data
  loading.value = false
}
// 搜索账号
// const onSearch = async () => {
//   // loading.value = true
//   const res = await searchAccount(formModel)
//   accountData.value = res.data.data
//   loading.value = false
// }
// 重置
// const onReset = async () => {
//   formModel.value.account = ''
//   getauthorizedaccount()
// }
// 添加厂区
// 重置添加厂房
const onResetFactory = () => {
  factoryName.value.factoryName = ''
  getFactoryList()
}
const addFactory = async () => {
  console.log(factoryName.value.factoryName)
  await addFactoryBuilding(factoryName.value)
  ElMessage.success('添加成功')
  onResetFactory()
}
// 获得厂房列表
const getFactoryList = async () => {
  loadingFactory.value = true
  const res = await getFactory()
  factoryList.value = res.data.data
  loadingFactory.value = false
}
const addfactoryDialog = () => {
  dialogFactory.value = true
  getFactoryList()
}
// 删除厂房
const deleteFactory = async (row) => {
  await ElMessageBox.confirm('此操作将删除该厂房, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  console.log(row)
  await deleteFactoryBuilding({ factoryId: row.factoryId })
  ElMessage.success('删除成功')
  getFactoryList()
}
// 重置车间
const onResetWorkShop = () => {
  workshopName.value.name = ''
  getWorkShopList()
}
// 添加车间
const addworkshop = async () => {
  await addWorkShop(workshopName.value)
  onResetWorkShop()
  ElMessage.success('添加成功')
}

// 获得车间列表
const getWorkShopList = async () => {
  // loading.value = true
  const res = await getWorkShop()
  workshopList.value = res.data.data
  loading.value = false
}
const addWorkShopDialog = () => {
  dialogWorkShop.value = true
  getWorkShopList()
}
// 删除车间
const deleteworkShop = async (row) => {
  await ElMessageBox.confirm('此操作将删除该车间, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  console.log(row)
  await deleteWorkShop({ id: row.id })
  getWorkShopList()
  ElMessage.success('删除成功')
}

const empower = (row) => {
  dialogVisible.value = true
  empowerData.value.userName = row.userName
}

const deletePermission = async (row) => {
  // 提示用户是否要删除
  await ElMessageBox.confirm('此操作将去除账号权限, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  })
  console.log(row)
  await deleteAccount(row)
  console.log(row)
  ElMessage.success('删除成功')
  // 重新渲染列表
  getauthorizedaccount()
}

// 授权
const onSubmit = async () => {
  console.log(empowerData.value)
  await ElMessageBox.confirm('此操作将授予账号权限, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  })
  await authorizeFactory(empowerData.value)
  ElMessage.success('授权成功')
  console.log(empowerData.value)
  empowerData.value.factoryId = ''
  empowerData.value.id = ''
  empowerData.value.userName = ''
  getauthorizedaccount()
}
getauthorizedaccount()
</script>
<template>
  <page-container title="权限管理" class="container">
    <template #extra>
      <el-button
        v-if="userStore.user.roleId === 1"
        @click="addfactoryDialog"
        class="add-button"
        >添加厂房</el-button
      >
      <el-button
        v-if="userStore.user.roleId === 2"
        @click="addWorkShopDialog"
        class="add-button"
        >添加车间</el-button
      >
    </template>
    <!-- <el-form inline>
      <el-form-item label="搜索账号">
        <el-input
          v-model="formModel.account"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" class="select-button">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-table
      v-loading="loading"
      style="width: 100%"
      height="550px"
      :data="accountData"
    >
      <el-table-column
        type="index"
        label="序号"
        width="100px"
      ></el-table-column>
      <el-table-column prop="userName" label="授权账号"></el-table-column>
      <el-table-column prop="perName" label="授权厂房"></el-table-column>
      <el-table-column label="授权级别">
        <template #default="{ row }">
          <span v-if="row.roleId === 0">未授权</span>
          <span v-else-if="row.roleId === 2">厂长</span>
          <span v-else-if="row.roleId === 3">车间经理</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            v-if="row.roleId === 0 || row.roleId === 3"
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="empower(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="deletePermission(row)"
          ></el-button>
        </template>
      </el-table-column>
      <!-- 没有数据 -->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 授权厂房车间 -->
    <el-dialog v-model="dialogVisible" title="授权" width="450px">
      <el-form label-width="100px" style="padding-right: 30px">
        <el-form-item label="厂房名称" v-if="userStore.user.roleId === 1">
          <el-select v-model="empowerData.factoryId" @click="getFactoryList">
            <el-option
              v-for="channel in factoryList"
              :key="channel.id"
              :label="channel.factoryName"
              :value="channel.factoryId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车间名称" v-if="userStore.user.roleId === 2">
          <el-select v-model="empowerData.id" @click="getWorkShopList">
            <el-option
              v-for="channel in workshopList"
              :key="channel.id"
              :label="channel.name"
              :value="channel.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit"> 授权</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 添加厂房 -->
    <el-dialog v-model="dialogFactory" title="添加厂房" width="450px">
      <el-form label-width="100px" style="padding-right: 30px">
        <el-form-item label="厂房名称">
          <el-input
            v-model="factoryName.factoryName"
            placeholder="请输入厂房名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="addFactory" class="select-button"
            >添加厂房</el-button
          >
          <el-button @click="onResetFactory">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loadingFactory"
        style="width: 100%"
        height="350px"
        :data="factoryList"
      >
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="factoryName" label="厂房"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              circle
              plain
              type="danger"
              :icon="Delete"
              @click="deleteFactory(row)"
            ></el-button>
          </template>
        </el-table-column>
        <!-- 没有数据 -->
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
    </el-dialog>
    <!--添加车间  -->
    <el-dialog v-model="dialogWorkShop" title="添加车间" width="450px">
      <el-form label-width="100px" style="padding-right: 30px">
        <el-form-item label="车间名称">
          <el-input
            v-model="workshopName.name"
            placeholder="请输入车间名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="addworkshop" class="select-button"
            >添加车间</el-button
          >
          <el-button @click="onResetWorkShop">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table style="width: 100%" height="350px" :data="workshopList">
        <el-table-column
          type="index"
          label="序号"
          width="100px"
        ></el-table-column>
        <el-table-column prop="name" label="车间"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              circle
              plain
              type="danger"
              :icon="Delete"
              @click="deleteworkShop(row)"
            ></el-button>
          </template>
        </el-table-column>
        <!-- 没有数据 -->
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
    </el-dialog>
  </page-container>
</template>
<style lang="scss" scoped>
.container {
  border-radius: 20px;
  font-weight: bold;
  font-size: 17px;
}
.add-button {
  background-color: #077aee;
  font-weight: bold;
  color: #fff;
}
.select-button {
  background-color: #077aee;
  color: #fff;
}
</style>
